package com.bankcomm.ui.specialwidget;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bankcomm.R;
import com.bankcomm.bean.Ticket;
import com.bankcomm.bean.TrainTicket;
import com.bankcomm.common.ConfigManager;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.BaseViewCopy;
import com.bankcomm.ui.specialwidget.LPTicketList.MyTicketList.MyAdapter;
import com.bankcomm.ui.views.IHeightListView;
import com.rytong.tools.ui.Component;
import com.rytong.tools.ui.LPScrollView;
import com.rytong.tools.utils.Utils;

public class LPTicketList extends Component {
	List<Ticket> ticket_list;
	MyAdapter adapter;
	@Override
	public void initRealView(Activity activity, String nodeValue) {
		realView_ = new MyTicketList(activity);
	}
	
	class MyTicketList extends IHeightListView implements CompositedComponent {
		/** 票据类型 */
		Ticket mTicket;
		String url;
		
		public MyTicketList(final Context context) {
			super(context);
			mTicket = new Ticket();
			
			setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			
			setDividerHeight(0);
			setDivider(new ColorDrawable(R.color.transparent));
			// 获得 data 属性的值, 取得所有的票
			Object objData = getPropertyByName("data");
			if (objData != null) {
				ticket_list = mTicket.parse((String)objData, Ticket.TRAIN_NUM);
			}
			
			adapter = loadingAdapter(context, ticket_list);
			setAdapter(adapter);
			
			/**
			 * OnItemClick点击事件
			 * */
			this.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int position, long arg3) {
					String url = ((TrainTicket)ticket_list.get(position)).getUrl();
					LPMid.getInstance().um_.dealWithLink(url, (BaseViewCopy)context);
				}
			});
			
			// 设置选中项的背景色。
			this.setSelector(R.drawable.list_item_bg);
		}
		
		/**
		 * 加载票类控件适配器
		 * 
		 * @param context
		 * @param ticketType
		 */
		private MyAdapter loadingAdapter(Context context, List<Ticket> list) {
			return new MyAdapter(context, list);
		}

		@Override
		public Component composited() {
			return LPTicketList.this;
		}
		
		class MyAdapter extends BaseAdapter {
			Context c;
			List<Ticket> list;
			
			public MyAdapter(Context context, List<Ticket> list) {
				c = context;
				this.list = list;
			}
			
			@Override
			public int getCount() {
				return list.size();
			}

			@Override
			public Object getItem(int position) {
				return list.get(position);
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ViewHolder holder = null;
				if (null == convertView) {
					convertView = LayoutInflater.from(c).inflate(R.layout.ticket_list_item, null);

					holder = new ViewHolder();
					holder.train_depart_date = (TextView) convertView.findViewById(R.id.train_depart_date);
					holder.train_arrive_date = (TextView) convertView.findViewById(R.id.train_arrive_date);
					holder.train_org = (TextView) convertView.findViewById(R.id.train_org);
					holder.train_dst = (TextView) convertView.findViewById(R.id.train_dst);
					holder.train_no = (TextView) convertView.findViewById(R.id.train_no);
					holder.train_type = (TextView) convertView.findViewById(R.id.train_type);
					holder.train_time = (TextView) convertView.findViewById(R.id.train_time);
					holder.train_distance = (TextView) convertView.findViewById(R.id.train_distance);
					holder.ticket_min_price = (TextView) convertView.findViewById(R.id.ticket_min_price);
					holder.ticket_max_price = (TextView) convertView.findViewById(R.id.ticket_max_price);

					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}

				holder.train_depart_date.setText(((TrainTicket)list.get(position)).getDepartDate());
				holder.train_arrive_date.setText(((TrainTicket)list.get(position)).getArriveDate());
				holder.train_org.setText(((TrainTicket)list.get(position)).getOrg());
				holder.train_dst.setText(((TrainTicket)list.get(position)).getDst());
				holder.train_no.setText(((TrainTicket)list.get(position)).getNo());
				holder.train_type.setText(((TrainTicket)list.get(position)).getType());
				holder.train_time.setText(((TrainTicket)list.get(position)).getTime());
				holder.train_distance.setText(((TrainTicket)list.get(position)).getDistance());
				holder.ticket_min_price.setText(((TrainTicket)list.get(position)).getTicket_min_price());
				holder.ticket_max_price.setText(((TrainTicket)list.get(position)).getTicket_max_price());
				return convertView;
			}
			
			class ViewHolder {
				TextView train_depart_date;
				TextView train_arrive_date;
				TextView train_org;
				TextView train_dst;
				TextView train_no;
				TextView train_type;
				TextView train_time;
				TextView train_distance;
				TextView ticket_min_price;
				TextView ticket_max_price;
			}
		}
		
	}
	
	@Override
	public void mouldH() throws Exception {
		super.mouldH();
		
		int totalHeight = 0;
		int childSize = (adapter != null) ? adapter.getCount() : 0;
		if(childSize > 0){
			for (int i = 0; i < childSize; i++) {
				View listItem = adapter.getView(i, null, (MyTicketList) this.realView_);
				listItem.measure(0, 0);
				totalHeight += listItem.getMeasuredHeight();
			}
			totalHeight += ((MyTicketList) this.realView_).getDividerHeight() * (childSize - 1);
			AbsoluteLayout.LayoutParams lp = (android.widget.AbsoluteLayout.LayoutParams) getLayoutParams();
			lp.height = totalHeight;
			setLayoutParams(lp);
		}
	}
	
	@Override
	public void releaseResource(Component component) {
		super.releaseResource(component);
		if(ticket_list != null){
			ticket_list.clear();
			ticket_list = null;
		}
	}
}
